package com.shuaqiu.sboot.login;

import com.shuaqiu.sboot.login.bean.LoginUserVo;
import com.shuaqiu.sboot.system.user.bean.UserBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class LoginService {

    private LoginMapper userMapper;

    @Autowired
    public void setUserMapper(final LoginMapper userMapper) {
        this.userMapper = userMapper;
    }


    @Transactional
    public LoginUserVo login(final String account, final String password) {
        final UserBo userBo = userMapper.selectUser(account);
        if (userBo == null || !password.equals(userBo.getPassword())) {
            return null;
        }

        userMapper.updateLoginTime(account, new Date());

        final LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setAccount(userBo.getAccount());
        loginUserVo.setName(userBo.getName());
        loginUserVo.setGender(userBo.getGender());
        loginUserVo.setLastLoginTime(new Date());
        return loginUserVo;
    }
}
